package com.great.meal.search.web;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.great.meal.search.dao.CustomerDao;
import com.great.meal.search.domain.Customer;
import com.great.meal.search.validator.CustomerValidator;

/**
 * @author bili
 *  客户的控制类
 */
@Controller
@RequestMapping("/customer.do")
public class CustomerController {
	
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private  CustomerDao customerDao;
	
	/**
	 *   判断客户名称是否已存在
	 * @param customerName
	 * @param response
	 * @return
	 */
	@RequestMapping(params= "method=isExist")
	public String isExist(@RequestParam("account") String account,HttpServletResponse response){
		PrintWriter out;
		try {

			
			out = response.getWriter();
			out.print("wwww".equals(account));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;		
	}
	/**
	 * 用户登录
	 * @param customer
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=loginCustomer")
	public String logonCustomer(HttpServletRequest request,HttpServletResponse response){
		
		return "mainLayout";
	}
	
	/**
	 * 注册客户
	 * @param customer
	 * @param result
	 * @param status
	 * @return
	 */
	@RequestMapping(params = "method=addCustomer")
	
	public String addCustomer(Customer customer, BindingResult result, SessionStatus status){
		new CustomerValidator().validate(customer, result, customerDao);
		if(result.hasErrors()){
			return "registerUser";
		} else{
			this.customerDao.saveOrUpdate(customer);
			status.setComplete();
			return "index";
		}
	}
	
	@RequestMapping(params = "method=getCustomer")
    public ModelMap getCustomer(@RequestParam("customerId") Long customerId){
		ModelMap map;
		try{
			map = new ModelMap(this.customerDao.getById(customerId));
			logger.info("load customer.do " + this.customerDao.getTClass());
			return map;
		} catch (Exception e){
			logger.info("load customer.do " + e + this.customerDao.getTClass());
			return null;
		}
	}
	/**
	 * 修改客户信息
	 *
	 * @param customer
	 * @return
	 */
	@RequestMapping(params = "method=editCustomer")
	public String editCustomer(@RequestParam("customerId") Long customerId,Customer customer){
		
		
		
		return null;
		
	}
	
	/**
	 * 删除客户
	 * @return
	 */
	@RequestMapping(params = "method=deleteCustomer")
	public String deleteCustomer(Customer customer){
		
		this.customerDao.delete(customer);
		
		return null;
		
	}
	
	
}
